package com.java2007.hotel.controller.front;

import com.alibaba.druid.util.StringUtils;
import com.java2007.hotel.controller.Controller;
import com.java2007.hotel.entity.PageBean;
import com.java2007.hotel.pojo.DinnerTable;
import com.java2007.hotel.pojo.Food;
import com.java2007.hotel.pojo.FoodType;
import com.java2007.hotel.service.DinnerTableService;
import com.java2007.hotel.service.FoodService;
import com.java2007.hotel.service.FoodTypeService;
import com.java2007.hotel.service.impl.DinnerTableServiceImpl;
import com.java2007.hotel.service.impl.FoodServiceImpl;
import com.java2007.hotel.service.impl.FoodtypeServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author Reckeys
 * @version V1.0
 * @date 2021/01/26
 **/
@WebServlet("/front")
public class FrontController extends Controller {
    private DinnerTableService dinnerTableService = new DinnerTableServiceImpl();
    private FoodTypeService foodTypeService = new FoodtypeServiceImpl();
    private FoodService foodService = new FoodServiceImpl();

    /**
     * 查询空闲的桌子
     * 将菜类别加载进session
     *
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String findDinnerTableByStatus(HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<DinnerTable> dinnerTableList = dinnerTableService.findDinnerTableByStatus(0);
        request.setAttribute("dinnerTableList", dinnerTableList);

        //加载菜类别
        HttpSession session = request.getSession();
        List<FoodType> foodTypeList = (List<FoodType>) session.getAttribute("FoodType");
        if (foodTypeList == null || foodTypeList.size() == 0) {
            foodTypeList = foodTypeService.findByTypeName("");
            session.setAttribute("foodTypeList", foodTypeList);
        }

        return "forward:/front/index.jsp";
    }

    /**
     * 通过'菜类别'和'菜名'    查询'菜'的信息
     * 如果菜类别和菜名为null 则查询所有
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String findFoodToPage(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //选择餐桌时，将餐桌id保存到session域中，用于后续订单生成
        //保存餐桌ID
        String tableId = request.getParameter("tableId");
        if (tableId != null) {
            int i = Integer.parseInt(tableId);
            request.getSession().setAttribute("tableId", i);
        }

        //获取页数
        String pageNoStr = request.getParameter("pageNo");
        //判断页数是否为空
        // 用于餐桌跳转到菜单
        if (StringUtils.isEmpty(pageNoStr)) {
            pageNoStr = "1";
        }
        Integer pageNo = Integer.valueOf(pageNoStr);

        //封装进food
        Map<String, String[]> parameterMap = request.getParameterMap();
        Food food = new Food();
        BeanUtils.populate(food, parameterMap);

        PageBean<Food> pageBean = foodService.findFoodToPage(pageNo, 6, food);
        request.setAttribute("pageBean", pageBean);
        request.setAttribute("foodName",food.getFoodName());
        request.setAttribute("foodTypeId",food.getFoodTypeId());
        return "forward:front/detail/caidan.jsp";


//        //创建Food实体类用于封装
//        Food food = new Food();
//        //获取菜类别id
//        String typeIdStr = request.getParameter("typeId");
//        //判断菜类别是否为空，非空：封装进food
//        // 用于查询菜后按菜类别分类
//        if (!StringUtils.isEmpty(typeIdStr)){
//            Integer typeId = Integer.valueOf(typeIdStr.trim());
//            food.setFoodTypeId(typeId);
//        }
//
//        //获取菜名
//        String foodName = request.getParameter("foodName");
//        //判断是否为空,非空：将菜名封装
//        if (!StringUtils.isEmpty(foodName)){
//            food.setFoodName(foodName.trim());
//        }
    }

    /**
     * 通过foodId查询菜的信息
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String findFoodByFoodId(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String foodIdStr = request.getParameter("foodId");
        if (null == foodIdStr){
            return "菜不存在";
        }
        Integer foodId = Integer.valueOf(foodIdStr);
        Food food = foodService.findByfoodId(foodId);
        request.setAttribute("food",food);
        return "forward:front/detail/caixiangxi.jsp";
    }


}